import Uploader from 'packages/BaseUpload/uploader';
import { Message, Loading } from 'element-ui'
import { t } from '@src/locales'
const MAX_SIZE = 50

export function getCustomPasteValue(e) {
  e.preventDefault();
  if (!(e.clipboardData && e.clipboardData.items)) {
    return false
  }

  for (var i = 0, len = e.clipboardData.items.length; i < len; i++) {
    var item = e.clipboardData.items[i];

    if (item.kind === 'string') {
      const text = e.clipboardData.getData('text/plain'); // 获取粘贴的纯文本
      return {
        type: 'text',
        value: text
      }
    } else if (item.kind === 'file' && item.type.includes('image')) {

      let pasteFile = item.getAsFile();
      return {
        type: 'file',
        fileType: 'image',
        value: pasteFile
      }
    }
  }
  return false;
}

// haveImage(vla){

// }

export async function uploadFile(file) {
  if (!file) return Message.error(t('common.placeholder.selectImportFile'))
  let errMessage = validate(file)

  if (errMessage) return Message.error(errMessage)
  let loading = Loading.service()

  try {
    const { status, data, message } = await Uploader.upload(file, '/files/upload')
    if (status == 0) {
      return data
    } else {
      Message.error(message)
      return null
    }
  } catch {
    return null
  } finally {
    loading.close()
  }
}

export function validate(file) {
  let fileName = file.name
  // 验证文件大小
  if (file.size > MAX_SIZE * 1024 * 1024) return t('common.base.uploadModal.fileSizeLowerTips', {size:`${MAX_SIZE}m`})
  // 验证是否有扩展名
  let lastDotIndex = fileName.lastIndexOf('.')
  if (lastDotIndex < 0) return t('common.base.uploadModal.unkonowFileType')
}

export function isImageNode(el) {
  return el?.nodeName == 'IMG'
}

// 单引号转双引号
export function doubleQuotesToSingleQuotes(val) {
  return val.replace(/\"/g, "'")
}

// 双引号转单引号
export function singleQuotesToDoubleQuotes(val) {
  return val.replace(/'/g, '"')
}

export function flatten(arr) {
  return arr.reduce((result, item) => {
    return result.concat(item, (Array.isArray(item.children) ? flatten(item.children) : []));
  }, []);
}

export function checkValIsEmpty(nodes = []) {
  let list = flatten(Array.isArray(nodes) ? nodes : [])
  let imgs = list.filter(v => v.type == 'image')

  let haveText = list.find(v => texIsEmpty(v.text))
  let haveImg = imgs.length > 0

  return !haveText && !haveImg
}

/** 获取发送类型 */
export function getMessageType(nodes = []) {

  let list = flatten(Array.isArray(nodes) ? nodes : [])
  let imgs = list.filter(v => v.type == 'image')

  let haveText = list.find(v => texIsEmpty(v.text))
  let haveImg = imgs.length > 0

  // 只有文本的情况
  if (haveText && !haveImg) {
    return 'txt'
  }

  // 只有图片的情况
  if (!haveText && haveImg) {
    if (imgs.length == 1) {
      let img = imgs[0]
      let href = img.href || JSON.stringify({})
      let dataHref = JSON.parse(href)
      if (dataHref?.isemoji) {
        // 表情包按富文本处理
        return 'paragraph'
      }
      return 'newImage'
    } else {
      return 'paragraph'
    }
  }

  return 'paragraph'
}

function texIsEmpty(val) {
  return val?.replace(/\ +/g, "")?.replace(/[\r\n]/g, "")
}